﻿using HPIT.RentHouse.Common;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entityes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RentHouse.Service
{
    public class HouseService : IHouseService
    {
        public List<HouseDTO> GetHouseList(string communityName, int typeId, int start, int length, ref int totalCount)
        {
            using(var db=new RentHouseEntity())
            {
                var bs = new BaseService<T_Houses>(db);
                var query = PredicateExtensions.True<T_Houses>();
                if (typeId > 0)//判断当前的房源类型
                {
                    query = query.And(e => e.TypeId == typeId);
                }//分页查询
                if (!string.IsNullOrWhiteSpace(communityName))
                {
                    query = query.And(e => e.T_Communities.Name.Contains(communityName));
                }
                var list = bs.GetPagedList(start, length, ref totalCount, query, e => e.Id, false).Select(e => new HouseDTO
                {
                    Address = e.Address,
                    Area = e.Area,
                    CommunityName = e.T_Communities.Name,
                    Id = e.Id,
                    MonthRent = e.MonthRent,
                    RegionName = e.T_Communities.T_Regions.Name,
                    RoomTypeId = e.RoomTypeId,
                    DecorateStatusId = e.DecorateStatusId
                }).ToList();
                var bsIdNames = new BaseService<T_IdNames>(db);
                //获取所有的数据字典
                var idNameList = bsIdNames.GetList(e => true).ToList();
                //获取数据字典中的信息
                list.ForEach(e =>
                {
                    e.DecorateStatusName = idNameList.FirstOrDefault(i => i.Id == e.DecorateStatusId).Name;
                    e.RoomTypeName = idNameList.FirstOrDefault(i => i.Id == e.RoomTypeId).Name;
                });
                return list;
            }
        }

        public List<AttachmentDTO> GetAttachmentList()
        {
            using (var db = new RentHouseEntity())
            {
                var bs = new BaseService<T_Attachments>(db);
                var list = bs.GetList(e => true).Select(e => new AttachmentDTO
                {
                    Id = e.Id,
                    Name = e.Name,
                    IconName = e.IconName
                }).ToList();
                return list;
            }
        }
        public AjaxResult AddHouse(HouseAddDTO dto)
        {
            using(var db=new RentHouseEntity())
            {
                var bs = new BaseService<T_Houses>(db);
                //房源信息
                var model = new T_Houses()
                {
                    Address=dto.Address,
                    Area=dto.Area,
                    CheckInDateTime=dto.CheckInDateTime,
                    CommunityId=dto.CommunityId,
                    CreateDateTime=DateTime.Now,
                    DecorateStatusId=dto.DecorateStatusId,
                    Description=dto.Description,
                    Direction=dto.Direction,
                    FloorIndex=dto.FloorIndex,
                    IsDeleted=dto.IsDeleted,
                    LookableDateTime=dto.LookableDateTime,
                    MonthRent=dto.MonthRent,
                    OwnerName=dto.OwnerName,
                    OwnerPhoneNum=dto.OwnerPhoneNum,
                    RoomTypeId=dto.RoomTypeId,
                    StatusId=dto.StatusId,
                    TotalFloorCount=dto.TotalFloorCount,
                    TypeId=dto.TypeId
                };
                var bsAttachment = new BaseService<T_Attachments>(db);
                //添加配套设施
                if (dto.AttachmentIds.Length > 0)
                {
                    for (int i = 0; i < dto.AttachmentIds.Length; i++)
                    {
                        var attachmentId = dto.AttachmentIds[i];
                        var attachment = bsAttachment.Get(e => e.Id == attachmentId);
                        if (attachment != null)
                        {
                            model.T_Attachments.Add(attachment);
                        }
                    }
                }
                if (bs.Add(model) > 0)
                {
                    return new AjaxResult(ResultState.Success, "添加成功");
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "添加失败");
                }
            }
        }

       
    }
}
